package com.tlvip.design.prototype.v2;

import java.io.*;

/**
 * <p>description：</p>
 * <p>
 * 使用java序列化实现clone，属于CPU密集型，不推荐使用
 *
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 2021/8/15</p>
 * Author mac
 *
 * @version 1.0
 */
public class TestProtoTypeV2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("-------------原始对象信息---------------");
        BaseInfo baseInfo = new BaseInfo("b1", 2);
        Product product = new Product("part1", "part2", 3, baseInfo);
        System.out.println(product);
        System.out.println(baseInfo);
        System.out.println("---------------克隆后的对象信息-------------");
        Product productClone = (Product) product.clone();
        System.out.println(productClone);
        System.out.println(productClone.getBaseInfo());
        System.out.println("---------------修改原始对象BaseInfo信息-------------");
        baseInfo.setB1("bb1");
        System.out.println(product);
        System.out.println(baseInfo);
        System.out.println("---------------修改原始对象BaseInfo信息后，查看clone对象信息-------------");
        System.out.println(productClone);
        System.out.println(productClone.getBaseInfo());

    }
}

class BaseInfo implements /*Cloneable,*/ Serializable {
    private static final long serialVersionUID = 123213453454398774L;
    private String b1;
    private Integer b2;

    public BaseInfo(String b1, Integer b2) {
        this.b1 = b1;
        this.b2 = b2;
    }

    public String getB1() {
        return b1;
    }

    public void setB1(String b1) {
        this.b1 = b1;
    }

    public Integer getB2() {
        return b2;
    }

    public void setB2(Integer b2) {
        this.b2 = b2;
    }

//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return (BaseInfo) super.clone();
//    }

    @Override
    public String toString() {
        return super.hashCode() + ",BaseInfo{" +
                "b1='" + b1 + '\'' +
                ", b2=" + b2 +
                '}';
    }
}

class Product implements /*Cloneable,*/ Serializable {

    private static final long serialVersionUID = 123454398721345374L;

    private String part1;
    private String part2;
    private Integer part3;
    private BaseInfo baseInfo;

    public Product(String part1, String part2, Integer part3, BaseInfo baseInfo) {
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.baseInfo = baseInfo;
    }

    public String getPart1() {
        return part1;
    }

    public void setPart1(String part1) {
        this.part1 = part1;
    }

    public String getPart2() {
        return part2;
    }

    public void setPart2(String part2) {
        this.part2 = part2;
    }

    public Integer getPart3() {
        return part3;
    }

    public void setPart3(Integer part3) {
        this.part3 = part3;
    }

    public BaseInfo getBaseInfo() {
        return baseInfo;
    }

    public void setBaseInfo(BaseInfo baseInfo) {
        this.baseInfo = baseInfo;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 使用序列化方式拷贝对象
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream)) {
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        try (ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream)) {
            Product clone = (Product) ois.readObject();
            return clone;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public String toString() {
        return super.hashCode() + ",Product{" +
                "part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3=" + part3 +
                ", baseInfo=" + baseInfo +
                '}';
    }
}
